package com.zznj.xzjc.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.view.dialog.RxDialogDownload;
import com.zznj.xzjc.ActivityAnJianDetail;
import com.zznj.xzjc.R;
import com.zznj.xzjc.adapter.LcjdAdapter;
import com.zznj.xzjc.adapter.index.FileDataAdapter;
import com.zznj.xzjc.adapter.index.FuJianAdapter;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.bean.FuJianBean;
import com.zznj.xzjc.bean.index.FileData;
import com.zznj.xzjc.event.EventTag;
import com.zznj.xzjc.event.MessageEvent;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.entity.BaseBean;
import com.zznj.xzjc.net.entity.DiaoChaQuZhengBean;
import com.zznj.xzjc.net.entity.JcjyxqResult;
import com.zznj.xzjc.net.entity.LoginBean;
import com.zznj.xzjc.net.entity.XsglxqResult;
import com.zznj.xzjc.net.rxjava.HttpMethods;
import com.zznj.xzjc.net.subscribers.ProgressSubscriber;
import com.zznj.xzjc.net.subscribers.SubscriberOnNextListener;
import com.zznj.xzjc.utils.FileUtils;
import com.zznj.xzjc.utils.StringUtils;
import com.zznj.xzjc.utils.TimeUtils;
import com.zznj.xzjc.utils.fileutils.OkManager;
import com.zznj.xzjc.view.MaxHeightRecyclerView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.w3c.dom.Text;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.FormBody;

/**
 * 描述：线索发现
 */
public class ActivityJcjyDetail extends BaseActivity {


    Unbinder unbinder;
    @BindView(R.id.jcjyNo)
    TextView jcjyNo;
    @BindView(R.id.jcjyTime)
    TextView jcjyTime;
    @BindView(R.id.jcjyContent)
    TextView jcjyContent;

    @BindView(R.id.hfjcjyLinear)
    View hfjcjyLinear;
    @BindView(R.id.hfdw)
    TextView hfdw;
    @BindView(R.id.xgqk)
    TextView xgqk;


    @BindView(R.id.hsjcjyLinear)
    View hsjcjyLinear;
    @BindView(R.id.hsdw)
    TextView hsdw;
    @BindView(R.id.hsjg)
    TextView hsjg;
    @BindView(R.id.dcqk)
    TextView dcqk;


    @BindView(R.id.jcjyTv)
    TextView jcjyTv;
    @BindView(R.id.jcjyList)
    RecyclerView jcjyList;
    @BindView(R.id.hfjcjyTv)
    TextView hfjcjyTv;
    @BindView(R.id.hftime)
    TextView hftime;
    @BindView(R.id.hfjcjyList)
    RecyclerView hfjcjyList;

    @BindView(R.id.hsjcjyTv)
    TextView hsjcjyTv;
    @BindView(R.id.hsTime)
    TextView hsTime;
    @BindView(R.id.hsjcjyList)
    RecyclerView hsjcjyList;


    @BindView(R.id.jcjyfsdw)
    TextView jcjyfsdw;
    @BindView(R.id.jcjyjsdw)
    TextView jcjyjsdw;
    @BindView(R.id.jcjyglaj)
    TextView jcjyglaj;


    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_jcjy_detail_layout;
    }

    private String proposalId;
    private String msgId;

    @Override
    public void initView() {
        proposalId = getIntent().getExtras().getString("proposalId");
        msgId = getIntent().getExtras().getString("msgId");
        setTitle("检察建议详情");
        initJcjy();
        initHfjcjy();
        initHsjcjy();
    }

    private FileDataAdapter jcjyAdapter;
    private List<FileData> jcjyDataList;
    private void initJcjy(){
        jcjyDataList = new ArrayList<>();
        ((SimpleItemAnimator) jcjyList.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        jcjyList.setLayoutManager(linearLayoutManager);
        jcjyAdapter = new FileDataAdapter(R.layout.gyss_item_filedata, jcjyDataList);
        jcjyList.setAdapter(jcjyAdapter);
        jcjyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                FileData fData = jcjyDataList.get(position);
                if("jpg,jpeg,png,gif".contains(fData.getFileName().substring(fData.getFileName().lastIndexOf(".") + 1))){
                    List<String> pList = new ArrayList<>();
                    pList.add(HttpMethods.BASE_File_URL+fData.getFileId());
                    Bundle bundle = new Bundle();
                    bundle.putInt("item", 0);
                    bundle.putSerializable("listPath", (Serializable) pList);
                    RxActivityTool.skipActivity(mContext, NetPicShowActivity.class, bundle);
                }else{
                    downFile(HttpMethods.BASE_File_URL+fData.getFileId(),fData.getFileName());
                }
            }
        });
    }

    private void downFile(String downUrl,String fileName){
        File pFile = new File(FileUtils.getRootFile(),fileName);
        if (!pFile.exists()) {
            initDownDialog();
            OkManager.getInstance().downLoadFile(downUrl, fileName, new Handler(){
                @Override
                public void handleMessage(@NonNull Message msg) {
                    downDialog.dismiss();
                    if(msg.what == 100){
                        RxToast.normal("下载成功！");
                        Bundle bundle = new Bundle();
                        bundle.putString("fileName",fileName);
                        RxActivityTool.skipActivity(activity, FileDisplayActivity.class,bundle);
                    }else if(msg.what == -1){
                        RxToast.normal("下载失败！");
                        pFile.delete();
                    }
                    super.handleMessage(msg);
                }
            });
        }else{
            Bundle bundle = new Bundle();
            bundle.putString("fileName",fileName);
            RxActivityTool.skipActivity(activity, FileDisplayActivity.class,bundle);
        }
    }
    private RxDialogDownload downDialog;
    private void initDownDialog() {
        downDialog = new RxDialogDownload(this);
        downDialog.setCanceledOnTouchOutside(false);
        downDialog.getTextView().setText("下载中...");
        downDialog.show();
    }

    private FileDataAdapter hfjcjyAdapter;
    private List<FileData> hfjcjyDataList;
    private void initHfjcjy(){
        hfjcjyDataList= new ArrayList<>();
        ((SimpleItemAnimator) hfjcjyList.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        hfjcjyList.setLayoutManager(linearLayoutManager);
        hfjcjyAdapter = new FileDataAdapter(R.layout.gyss_item_filedata, hfjcjyDataList);
        hfjcjyList.setAdapter(hfjcjyAdapter);
        hfjcjyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                FileData fData = hfjcjyDataList.get(position);
                if("jpg,jpeg,png,gif".contains(fData.getFileName().substring(fData.getFileName().lastIndexOf(".") + 1))){
                    List<String> pList = new ArrayList<>();
                    pList.add(HttpMethods.BASE_File_URL+fData.getFileId());
                    Bundle bundle = new Bundle();
                    bundle.putInt("item", 0);
                    bundle.putSerializable("listPath", (Serializable) pList);
                    RxActivityTool.skipActivity(mContext, NetPicShowActivity.class, bundle);
                }else{
                    downFile(HttpMethods.BASE_File_URL+fData.getFileId(),fData.getFileName());
                }
            }
        });
    }


    private FileDataAdapter hsjcjyAdapter;
    private List<FileData> hsjcjyDataList;
    private void initHsjcjy(){
        hsjcjyDataList = new ArrayList<>();
        ((SimpleItemAnimator) hsjcjyList.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        hsjcjyList.setLayoutManager(linearLayoutManager);
        hsjcjyAdapter = new FileDataAdapter(R.layout.gyss_item_filedata, hsjcjyDataList);
        hsjcjyList.setAdapter(hsjcjyAdapter);
        hsjcjyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                FileData fData = hsjcjyDataList.get(position);
                if("jpg,jpeg,png,gif".contains(fData.getFileName().substring(fData.getFileName().lastIndexOf(".") + 1))){
                    List<String> pList = new ArrayList<>();
                    pList.add(HttpMethods.BASE_File_URL+fData.getFileId());
                    Bundle bundle = new Bundle();
                    bundle.putInt("item", 0);
                    bundle.putSerializable("listPath", (Serializable) pList);
                    RxActivityTool.skipActivity(mContext, NetPicShowActivity.class, bundle);
                }else{
                    downFile(HttpMethods.BASE_File_URL+fData.getFileId(),fData.getFileName());
                }
            }
        });
    }

    @Override
    protected void initData() {
        getData();
        if(!StringUtils.isEmpty(msgId)){
            readMsg();
        }
    }


    private String caseId;
    private String caseState;
    private void getData() {
        SubscriberOnNextListener<BaseBean<JcjyxqResult>> onNextListener = baseBean -> {
            jcjyNo.setText(baseBean.getResult().getProposalReceive().getDocNo());
            jcjyTime.setText(TimeUtils.getTime(baseBean.getResult().getProposalReceive().getSendTime(),"yyyy-MM-dd HH:mm:ss"));
            jcjyContent.setText(baseBean.getResult().getProposalReceive().getSendContent());
            jcjyfsdw.setText(baseBean.getResult().getProposalReceive().getSendOrgName());
            jcjyjsdw.setText(baseBean.getResult().getProposalReceive().getReceiveOrgName());
            if(StringUtils.isEmpty(baseBean.getResult().getProposalReceive().getCaseNo())){
                jcjyglaj.setText("无");
                jcjyglaj.setTextColor(getResources().getColor(R.color.color_333333));
            }else{
                jcjyglaj.setTextColor(getResources().getColor(R.color.title_bg));
                jcjyglaj.setText(baseBean.getResult().getProposalReceive().getCaseNo());
            }
            caseId = baseBean.getResult().getProposalReceive().getCaseId();

            if(baseBean.getResult().getReceiveFiles() != null && baseBean.getResult().getReceiveFiles().size() > 0){
                jcjyDataList.clear();
                jcjyDataList.addAll(baseBean.getResult().getReceiveFiles());
                jcjyAdapter.notifyDataSetChanged();
                jcjyTv.setVisibility(View.GONE);
                jcjyList.setVisibility(View.VISIBLE);
            }else{
                jcjyTv.setVisibility(View.VISIBLE);
                jcjyList.setVisibility(View.GONE);
            }

            if(baseBean.getResult().getProposalReply() == null){
                hfjcjyLinear.setVisibility(View.GONE);
            }else{
                hfjcjyLinear.setVisibility(View.VISIBLE);
                hfdw.setText(baseBean.getResult().getProposalReply().getReceiveOrgName());
                hftime.setText(TimeUtils.getTime(baseBean.getResult().getProposalReply().getReplyCorrectTime(),"yyyy-MM-dd"));
                xgqk.setText(baseBean.getResult().getProposalReply().getReplyContent());

                if(baseBean.getResult().getReplyFiles() != null && baseBean.getResult().getReplyFiles().size() > 0){
                    hfjcjyDataList.clear();
                    hfjcjyDataList.addAll(baseBean.getResult().getReplyFiles());
                    hfjcjyAdapter.notifyDataSetChanged();
                    hfjcjyTv.setVisibility(View.GONE);
                    hfjcjyList.setVisibility(View.VISIBLE);
                }else{
                    hfjcjyTv.setVisibility(View.VISIBLE);
                    hfjcjyList.setVisibility(View.GONE);
                }
            }

            if(baseBean.getResult().getProposalVerify() == null){
                hsjcjyLinear.setVisibility(View.GONE);
            }else{
                hsjcjyLinear.setVisibility(View.VISIBLE);
                hsdw.setText(baseBean.getResult().getProposalVerify().getReceiveOrgName());
                hsTime.setText(TimeUtils.getTime(baseBean.getResult().getProposalVerify().getVerifyCorrectTime(),"yyyy-MM-dd"));
                hsjg.setText("1".equals(baseBean.getResult().getProposalVerify().getCorrect())?"已纠正":"未纠正");
                dcqk.setText(baseBean.getResult().getProposalVerify().getVerifyContent());

                if(baseBean.getResult().getVerifyFiles()!= null && baseBean.getResult().getVerifyFiles().size() > 0){
                    hsjcjyDataList.clear();
                    hsjcjyDataList.addAll(baseBean.getResult().getVerifyFiles());
                    hsjcjyAdapter.notifyDataSetChanged();
                    hsjcjyTv.setVisibility(View.GONE);
                    hsjcjyList.setVisibility(View.VISIBLE);
                }else{
                    hsjcjyTv.setVisibility(View.VISIBLE);
                    hsjcjyList.setVisibility(View.GONE);
                }
            }
        };
        HttpMethods.getInstance().sendDetail(new ProgressSubscriber<>(onNextListener, mContext, true), proposalId);
    }

    private void readMsg() {
        SubscriberOnNextListener<BaseBean<XsglxqResult>> onNextListener = baseBean -> {

        };
        FormBody.Builder builder = new FormBody.Builder()
                .add("msgId", msgId);
        HttpMethods.getInstance().updStatus(new ProgressSubscriber<>(onNextListener, mContext, true), builder.build());
    }

    @OnClick({R.id.jcjyglaj})
    public void onClick(View v){
        if(v.getId() == R.id.jcjyglaj){
            if(!StringUtils.isEmpty(caseId)){
                Bundle bundle = new Bundle();
                bundle.putString("caseId", caseId);
                RxActivityTool.skipActivity(mContext, ActivityAnJianDetail.class, bundle);
            }
        }
    }
}
